<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">

        <el-form-item>
          <span>状态：</span>
          <el-radio-group v-model="selectItem.status" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in statusMaps" :label="item.value" name="status" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <span>类型：</span>
          <el-radio-group v-model="selectItem.order_type" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in orderTypeOptions" :label="item.value" name="order_type" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <span>时间段：</span>
          <el-date-picker v-model="choseDate" type="daterange" placeholder="选择日期范围" @change="chose_date"></el-date-picker>
          <!--<span style="color: red;">默认昨日零时到今日23:59:59</span>-->
        </el-form-item>

        <div style="display: inline-block;float: right">
          <el-form :inline="true">
            <el-select v-model="selectItem.query" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-form-item>
              <el-input :placeholder="selectItem.query" v-model="selectItem.keywords" @keyup.enter.native="doSearch" style="width: 120px;"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="doSearch">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
            </el-form-item>
          </el-form>
        </div>

      </el-form>
    </el-col>

    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 20px;">
      <el-form :inline="true">
        <el-button :plain="true" type="success" @click="_importShow('plant')">导入菜品记录</el-button>
        <el-button :plain="true" type="success" @click="_importShow('goods')">导入商品记录</el-button>
        <el-button :plain="true" type="success" @click="_importShow('card')">导入虚拟卡商品记录</el-button>
        <el-button :plain="true" type="warning" @click="handleAdd">手动添加发货记录</el-button>
        <el-button :plain="true" type="danger" @click="batchUpdate" v-if="selectItem.status == -1 || selectItem.status == 0 || selectItem.status == 1 || selectItem.status == 2">批量修改包裹信息</el-button>
        <el-button :plain="true" type="danger" @click="setStatus" v-if="selectItem.status == 2">设置发货状态</el-button>
        <el-button :plain="true" type="info" @click="checkJd" v-if="selectItem.status == -1 || selectItem.status == 0">是否可京配</el-button>
        <el-button :plain="true" type="success" @click="submitToJD" v-if="selectItem.status == 1">递交到京东</el-button>
        <el-button type="success" @click="createNote" v-if="selectItem.status == 2 || selectItem.status == 3">生成运单</el-button>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="erpData" highlight-current-row v-loading="loading" @selection-change="selectionChange"
              style="width: 100%;" element-loading-text="玩命请求中。。。">

      <el-table-column type="selection" width="55">
      </el-table-column>

      <el-table-column prop="id" label="ID" width="80">
      </el-table-column>

      <el-table-column prop="uid" label="UID" width="80">
      </el-table-column>

      <!--<el-table-column prop="sn" label="期数" width="80">-->
      <!--</el-table-column>-->

      <<el-table-column prop="farm_id" label="农场" width="100" :formatter="farmName">
      </el-table-column>
      <el-table-column label="收货信息" width="300">
        <template slot-scope="scope">
          <p>{{ scope.row.receive_city }}{{ scope.row.receive_district }}{{ scope.row.receive_address }}</p>
          <p>{{ scope.row.receive_name }}   {{ scope.row.receive_mobile }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="delivery_id" label="运单号" width="150">
      </el-table-column>
      <el-table-column prop="order_id" label="订单号" width="100">
      </el-table-column>
      <el-table-column prop="mark" label="备注" width="190">
      </el-table-column>
      <el-table-column label="包裹信息" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.per_weight / 500 }} 斤 * {{ scope.row.package_count }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="product_num" label="物品数量" width="100">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="90" :formatter="showStatus">
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="150" :formatter="createTime">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div style="margin-left: 5px;">
            <a :href="'https://www.baidu.com/s?wd=' + scope.row.delivery_id" target="_blank" v-if="selectItem.status == 2 || selectItem.status == 3">查看物流</a>
            <el-button size="small" @click="editAddress(scope.row)" v-if="selectItem.status == 0 || selectItem.status == -1 || selectItem.status == -2">编辑</el-button>
            <el-button size="small" @click="updateDeliveryStatus(scope.row.id)" v-if="selectItem.status == 0">无法配送</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!--导入菜品记录弹窗-->
    <el-dialog title="导入菜品记录" v-model="importPlantShow" :close-on-click-modal="false">
      <el-form label-width="80px">
        <el-form-item label="区域">
          <template slot-scope="scope">
            <el-select v-model="area_name" placeholder="请选择大区域" size="large" @change="changeSelectArea">
              <el-option
                v-for="(item, index) in areaNames"
                :key="index"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="地块">
          <template slot-scope="scope">
            <el-select v-model="checkedName" multiple placeholder="选择小区域" size="large">
              <el-option v-for="(item, index) in mapNames" :key="index" :label="item" :value="item"></el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="importPlantShow = false">取消</el-button>
        <el-button type="primary" @click.native="importRecord()">导入</el-button>
      </div>
    </el-dialog>

    <!--导入商品记录弹窗-->
    <el-dialog title="导入商品记录" v-model="importGoodsShow" :close-on-click-modal="false">
      <el-form label-width="80px">
        <el-form-item label="发货日期">
          <template slot-scope="scope">
            <el-date-picker v-model="importGoodsDate.startTime" type="date" placeholder="选择开始日期"></el-date-picker>
            <span style="padding: 0px 5px;">-</span>
            <el-date-picker v-model="importGoodsDate.endTime" type="date" placeholder="选择结束日期"></el-date-picker>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="importGoodsShow = false">取消</el-button>
        <el-button type="primary" @click.native="importRecord20('goods')">导入</el-button>
      </div>
    </el-dialog>

    <!--导入卡片记录弹窗-->
    <el-dialog title="导入卡片记录" v-model="importCardShow" :close-on-click-modal="false">
      <el-form label-width="80px">
        <el-form-item label="日期">
          <template slot-scope="scope">
            <el-button @click.native="importRecord20(1)">星期一</el-button>
            <el-button @click.native="importRecord20(2)">星期二</el-button>
            <el-button @click.native="importRecord20(3)">星期三</el-button>
            <el-button @click.native="importRecord20(4)">星期四</el-button>
            <el-button @click.native="importRecord20(5)">星期五</el-button>
            <el-button @click.native="importRecord20(6)">星期六</el-button>
            <el-button @click.native="importRecord20(0)">星期日</el-button>
          </template>
        </el-form-item>
        <el-form-item label="说明">
          <template slot-scope="scope">
            <span style="color: red;">***如果相对今天为过去周天,则默认选择的为下周周天</span>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="importCardShow = false">取消</el-button>
      </div>
    </el-dialog>

    <!--手动添加发货记录-->
    <el-dialog title="手动添加发货记录" v-model="handleAddShow" :close-on-click-modal="false">
      <el-form label-width="80px">

        <div class="h-top">
          <el-form-item label="期号">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.sn" placeholder="请输入期号"></el-input>
            </template>
          </el-form-item>
        </div>

        <div class="h-top">
          <el-form-item label="UID">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.uid" placeholder="请输入UID"></el-input>
            </template>
          </el-form-item>
        </div>

        <div class="h-top">
          <el-form-item label="订单号">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.order_id" placeholder="请输入订单号"></el-input>
            </template>
          </el-form-item>
        </div>

        <el-form-item label="省份">
          <template slot-scope="scope">
            <citySelect ref="city_district"></citySelect>
          </template>
        </el-form-item>

        <el-form-item label="地址">
          <template slot-scope="scope">
            <el-input v-model="handleAddForm.express_info.edit" placeholder="请输入详细地址"></el-input>
          </template>
        </el-form-item>

        <div class="h-top" style="width: 320px;">
          <el-form-item label="姓名">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.express_info.name" placeholder="请输入姓名"></el-input>
            </template>
          </el-form-item>
        </div>

        <div class="h-top" style="width: 320px;">
          <el-form-item label="电话">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.express_info.phone" placeholder="请输入手机号"></el-input>
            </template>
          </el-form-item>
        </div>

        <div class="h-top">
          <el-form-item label="包裹数">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.package_count" placeholder="请输入包裹数"></el-input>
            </template>
          </el-form-item>
        </div>

        <div class="h-top">
          <el-form-item label="包裹重(斤)">
            <template slot-scope="scope">
              <el-input v-model="handleAddForm.per_weight" placeholder="请输入包裹重量"></el-input>
            </template>
          </el-form-item>
        </div>

        <el-form-item label="注意：">
          <template slot-scope="scope">
            <span style="display: block;color: red">1.uid和订单号可以不填，默认为0</span>
            <span style="display: block;color: red">2.包裹数量默认为1</span>
            <span style="display: block;color: red">3.每个包裹重量可以不填，默认为0，填入数字，支持小数，单位为斤</span>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleAddShow = false">取消</el-button>
        <el-button type="primary" @click.native="handleAddRecord">添加</el-button>
      </div>
    </el-dialog>

    <!--编辑收货信息弹窗-->
    <el-dialog title="手动添加发货记录" v-model="editShow" :close-on-click-modal="false">
      <el-form label-width="80px">

        <el-form-item label="省份">
          <template>
            <citySelect :province="editForm.express_info.province" :city="editForm.express_info.city" :district="editForm.express_info.district" ref="edit_city_select"></citySelect>
          </template>
        </el-form-item>

        <el-form-item label="地址">
          <template>
            <el-input v-model="editForm.express_info.edit" placeholder="请输入详细地址"></el-input>
          </template>
        </el-form-item>

        <div class="h-top" style="width: 320px;">
          <el-form-item label="姓名">
            <template>
              <el-input v-model="editForm.express_info.name" placeholder="请输入姓名"></el-input>
            </template>
          </el-form-item>
        </div>

        <div class="h-top" style="width: 320px;">
          <el-form-item label="电话">
            <template>
              <el-input v-model="editForm.express_info.phone" placeholder="请输入手机号"></el-input>
            </template>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editShow = false">取消</el-button>
        <el-button type="primary" @click.native="editAddressInfo">提交</el-button>
      </div>
    </el-dialog>

    <!--批量修改包裹数量弹框-->
    <el-dialog title="批量修改包裹信息" v-model="batchUpdateShow" :close-on-click-modal="false">
      <el-form label-width="80px">
        <el-form-item label="包裹数">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="updatePackageInfo.package_count" placeholder="请输入包裹数"></el-input>
            </div>
            <span style="margin-left: 10px;color: red;">输入数字(支持大于0的自然数)</span>
          </template>
        </el-form-item>
        <el-form-item label="包裹重">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="updatePackageInfo.per_weight" auto-complete="on"></el-input>
            </div>
            <span style="margin-left: 10px;color: red;">单位斤，输入数字(支持小数)</span>
          </template>
        </el-form-item>
        <el-form-item label="注意：">
          <template slot-scope="scope">
            <span style="color: red">具体修改是根据选择的记录来确定</span>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="batchUpdateShow = false">取消</el-button>
        <el-button type="primary" @click.native="batchUpdateSubmit">修改</el-button>
      </div>
    </el-dialog>
    
    <!--订单列表弹窗-->
    <el-dialog title="手动添加发货记录" v-model="printShow" :close-on-click-modal="false">
      <el-form label-width="80px">

        <el-form-item label="订单列表">
          <template>
            <a :href="item" target="_blank" v-for="(item, index) in orderUrl" style="margin-right: 20px;">生成订单{{ index + 1 }}</a>
          </template>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="printShow = false">关闭</el-button>
      </div>
    </el-dialog>

    <!--分页-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectItem.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>

<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
  import citySelect from '../common/citySelect'
  import { apiTree } from '../../apiTree'

  export default {
    name: 'erp',
    // 局部注册组件
    components: {
      citySelect
    },
    data() {
      return {
        statusMaps: [
          {value: '0', label: '待操作'},
          {value: '-1', label: '人工分拣'},
          {value: '1', label: '可配送'},
          {value: '2', label: '已递交'},
          {value: '3', label: '已发货'},
          {value: '-2', label: '无法配送'}
        ],
        options: [
          {value: 'product_num', label: '物品数量'},
          {value: 'receive_district', label: '区域'},
          {value: 'receive_mobile', label: '手机号'},
          {value: 'mark', label: '菜品'},
          {value: 'uid', label: 'uid'},
          {value: 'receive_name', label: '姓名'}
        ],
        orderTypeOptions: [
          {value: '0', label: '全部'},
          {value: '1', label: '菜品'},
          {value: '2', label: '商品'},
          {value: '4', label: '虚拟卡'}
        ],
        selectItem: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          query: this.$route.query.query !== undefined ? this.$route.query.query : '',
          keywords: this.$route.query.keywords !== undefined ? this.$route.query.keywords : '',
          status: this.$route.query.status !== undefined ? this.$route.query.status : 0,
          order_type: this.$route.query.order_type !== undefined ? this.$route.query.order_type : 0,
          startTime: this.$route.query.startTime !== undefined ? this.$route.query.startTime : 0,
          endTime: this.$route.query.endTime !== undefined ? this.$route.query.endTime : 0
        },
        importPlantShow: false, // 导入菜品记录弹窗
        importGoodsShow: false, // 导入商品记录弹窗
        importCardShow: false, // 导入卡片记录弹窗
        choseDate: [], // 选择日期范围
        importGoodsDate: {
          startTime: '',
          endTime: '',
        },
        loading: false,
        total: 0,
        erpData: [],
        areaNames: [], // 区域名称
        mapNames: [], // 地块名
        area_name: '', // 单区域名
        checkedName: [], // 多区域名称
        handleAddShow: false, // 手动添加弹窗
        handleAddForm: {
          sn: 0,
          uid: 0,
          order_id: 0,
          express_info: {
            phone: '',
            province: '',
            city: '',
            district: '',
            edit: '',
            name: ''
          },
          package_count: 1, // 包裹数量
          per_weight: 0 // 包裹均重，单位为g(显示用斤显示)
        },
        editShow: false,
        editForm: {
          id: 0,
          express_info: {
            phone: '',
            province: '',
            city: '',
            district: '',
            edit: '',
            name: ''
          }
        },
        batchUpdateShow: false, // 批量修改包裹数量弹窗
        multipleSelection: [], // 批量修改选中的项目
        updatePackageInfo: {
          per_weight: 0,
          package_count: 1
        },
        printShow: false, // 打印订单
        orderUrl: [] // 订单列表url
      }
    },
    methods: {
      // 选择状态
      currentStatusChange() {
        this.$router.push({path: this.$route.path, query: this.selectItem});
      },
      // 选择页码
      currentPageChange(val) {
        this.selectItem.page = val
        this.$router.push({path: this.$route.path, query: this.selectItem})
      },
      // 搜索
      doSearch() {
        if (this.selectItem.query) {
          this.$router.push({path: this.$route.path, query: this.selectItem})
        }
      },
      // 刷新页面
      _reload() {
        this.getErpList();
      },
      // 农场名称
      farmName(row, column) {
        return row.farm_id === '1' ? '枫泾农场' : '其他农场'
      },
      // 显示对应的状态
      showStatus(row) {
        if (row.status === '0') {
          return '待操作';
        } else if (row.status === '-1') {
          return '人工分拣';
        } else if (row.status === '-2') {
          return '无法配送';
        } else if (row.status === '1') {
          return '可配送';
        } else if (row.status === '2') {
          return '已递交';
        } else if (row.status === '3') {
          return '已发货';
        }
      },
      // 创建时间
      createTime(row) {
        return util.formatDate.format(new Date(row.create_time * 1000), 'yyyy-MM-dd hh:mm:ss')
      },
      // 选择日期搜索
      chose_date() {
        if (!this.choseDate[0] || !this.choseDate[1]) {
          return;
        }
        this.selectItem.startTime = Math.ceil(new Date(this.choseDate[0]).getTime() / 1000);
        this.selectItem.endTime = Math.ceil(new Date(this.choseDate[1]).getTime() / 1000);
        this.$router.push({path: this.$route.path, query: this.selectItem});
      },
      // 批量修改选择项
      selectionChange(val) {
        this.multipleSelection = val;
      },
      // 编辑包裹信息
      editAddress(row) {
        this.editShow = true;
        this.editForm = {
          id: row.id,
          express_info: {
            phone: row.receive_mobile,
            province: row.receive_province,
            city: row.receive_city,
            district: row.receive_district,
            edit: row.receive_address,
            name: row.receive_name
          }
        }
      },
      // 编辑包裹信息提交
      editAddressInfo() {
        let _address = this.$refs.edit_city_select.$data.address;
        if (util.isEmpty(_address.city) || util.isEmpty(_address.district) || util.isEmpty(_address.province)) {
          this.$message.error('省份错误！');
          return
        }
        let _express = this.editForm.express_info;
        if (util.isEmpty(_express.phone) || !util.isInt(_express.phone)) {
          this.$message.error('手机号错误！');
          return
        }
        if (util.isEmpty(_express.edit)) {
          this.$message.error('详细地址为空！');
          return
        }
        if (util.isEmpty(_express.name)) {
          this.$message.error('收货人姓名为空！');
          return
        }
        let _p = _address.province.split(',');
        let _c = _address.city.split(',');
        this.editForm.express_info.province = _p[0];
        this.editForm.express_info.city = _c[0];
        this.editForm.express_info.district = _address.district;
        apiTree('erp/index', 'erp/updateReceiveInfo', {data: this.editForm}, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '修改成功',
              type: 'success'
            });
          } else {
            this.$message.error(res.err_str);
          }
        })
      },
      // 导入记录显示
      _importShow(_type) {
        if (_type === 'plant') {
          this.importPlantShow = true;
        }
        if (_type === 'goods') {
          this.importGoodsShow = true;
        }
        if (_type === 'card') {
          this.importCardShow = true;
        }
      },
      // 导入菜品记录提交
      importRecord() {
        if (util.isEmpty(this.area_name) || util.isEmpty(this.checkedName)) {
          this.$message.error('信息为空');
          return
        }
        let param = {'area_name': this.area_name, 'name': this.checkedName};
        apiTree('erp/index', 'erp/importRecord', param, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '导入成功',
              type: 'success'
            });
            this.importShow = false;
            this.getErpList();
          } else {
            this.$message.error(res.err_str);
          }
        })
      },
      // 导入商品或者卡片记录提交
      importRecord20(_type) {
      	let _t = {};
        if (_type === 'goods') {
          if (this.importGoodsDate.startTime === '' || this.importGoodsDate.endTime === '') {
            this.$message.error('请选择时间!');
            return
          }
          _t.startTime = Math.ceil(new Date(this.importGoodsDate.startTime).getTime() / 1000);
          _t.endTime = Math.ceil(new Date(this.importGoodsDate.endTime).getTime() / 1000);
          if (_t.startTime > _t.endTime) {
            this.$message.error('时间选择错误!');
            return
          }
        }
        let param = {'type': _type, 'startTime': util.isEmpty(_t) ? '' : _t.startTime, 'endTime':  util.isEmpty(_t) ? '' : _t.endTime, 'startWeekDay': util.isInt(_type) ? _type : '-1'};
        if (_type === 'goods') {
          this.importRecord20Sub(_type, param);
        } else {
          this.$confirm('确定是导入的周天吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.importRecord20Sub(_type, param);
          }).catch(() => {});
        }
      },
      importRecord20Sub(_type, param) {
        apiTree('erp/index', 'erp/importRecord20', param).then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '导入成功',
              type: 'success'
            });
            if (_type === 'goods') {
              this.importGoodsShow = false;
            } else {
              this.importCardShow = false;
            }
            this.getErpList();
          } else {
            this.$message.error(res.err_str);
          }
        })
      },
      // 设置无法配送状态
      updateDeliveryStatus(delivery_id) {
        if (util.isEmpty(delivery_id) || !util.isInt(delivery_id)) {
          return
        }
        apiTree('erp/index', 'erp/updateDeliveryStatus', {'delivery_id': delivery_id}).then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.getErpList();
          }
        });
      },
      // 递交给京东
      submitToJD() {
        if (this.multipleSelection.length === 0) {
          this.$message.error('请选择要操作的项目!');
          return
        }
        let ids = this.multipleSelection.map(item => item.id).toString();
        this.loading = true;
        apiTree('erp/index', 'erp/submitToJD', {'id': ids}, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '递交成功',
              type: 'success'
            });
            this.loading = false;
            this.multipleSelection = [];
            this.getErpList();
          }
        });
      },
      // 添加发货记录显示
      handleAdd() {
        this.handleAddShow = true;
      },
      // 添加发货记录提交
      handleAddRecord() {
        let _address = this.$refs.city_district.$data.address;
        if (util.isEmpty(_address.city) || util.isEmpty(_address.district) || util.isEmpty(_address.province)) {
          this.$message.error('省份错误！');
          return
        }
        if (!util.isInt(this.handleAddForm.sn) || this.handleAddForm.sn === 0) {
          this.$message.error('期号错误！');
          return
        }
        if (util.isEmpty(this.handleAddForm.express_info.name) || util.isEmpty(this.handleAddForm.express_info.phone || util.isEmpty(this.handleAddForm.express_info.edit))) {
          this.$message.error('地址错误！');
          return
        }
        if ((this.handleAddForm.per_weight !== 0 && !util.isFloat(this.handleAddForm.per_weight, true))) {
          this.$message.error('包裹重量错误!');
          return
        }
        let _p = _address.province.split(',');
        let _c = _address.city.split(',');
        this.handleAddForm.express_info.province = _p[0];
        this.handleAddForm.express_info.city = _c[0];
        this.handleAddForm.express_info.district = _address.district;
        apiTree('erp/index', 'erp/addRecord', {data: this.handleAddForm}, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '导入成功',
              type: 'success'
            });
            this.handleAddShow = false;
            this.getErpList();
          } else {
            this.$message.error(res.err_str);
          }
        });
      },
      // 批量修改包裹信息显示
      batchUpdate() {
        this.batchUpdateShow = true;
      },
      // 批量修改包裹信息提交
      batchUpdateSubmit() {
        if (this.multipleSelection.length === 0) {
          this.$message.error('请选择要修改的项目!');
          return
        }
        if (util.isEmpty(this.updatePackageInfo.package_count) || !util.isInt(this.updatePackageInfo.package_count) || this.updatePackageInfo.package_count < 1) {
          this.$message.error('包裹数量错误!');
          return
        }
        if ((this.updatePackageInfo.per_weight !== 0 && !util.isFloat(this.updatePackageInfo.per_weight, true))) {
          this.$message.error('包裹重量错误!');
          return
        }
        let ids = this.multipleSelection.map(item => item.id).toString();
        let param = {
          per_weight: this.updatePackageInfo.per_weight,
          package_count: this.updatePackageInfo.package_count,
          ids: ids
        }
        apiTree('erp/index', 'erp/updatePackage', {'data': param}, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.batchUpdateShow = false;
            this.multipleSelection = [];
            this.getErpList();
          } else {
            this.$message.error(res.err_str);
          }
        });
      },
      // 检测是否可以京配
      checkJd() {
        if (this.multipleSelection.length === 0) {
          this.$message.error('请选择要检测的项目!');
          return
        }
        let ids = this.multipleSelection.map(item => item.id).toString();
        this.loading = true;
        apiTree('erp/index', 'erp/checkJdSend', {'id': ids}, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '检测成功',
              type: 'success'
            });
            this.loading = false;
            this.batchUpdateShow = false;
            this.multipleSelection = [];
            this.getErpList();
          }
        });
      },
      // 生成订单
      createNote() {
        this.orderUrl = [];
        let _num = Math.ceil(this.total / 100);
        let _len = this.erpData.length;
        let _where = [
          {'field': 'create_time', 'val': this.selectItem.startTime, 'self_op': '>=', 'union_op': 'and'},
          {'field': 'create_time', 'val': this.selectItem.endTime, 'self_op': '<=', 'union_op': 'and'}
        ];
        if (this.selectItem.query && this.selectItem.keywords) {
          _where.push({'field': this.selectItem.query, 'val': this.selectItem.keywords, 'self_op': '=', 'union_op': 'and'});
        }
        let _url = 'http://erpapi.kaikai001.com:815/?a=print&c=delivery';
        for (let i = 1; i <= _num; i++) {
          let _offset = (i - 1) * _len;
          this.orderUrl.push(_url + '&where=' + JSON.stringify(_where) + '&limit=' + _offset + ',' + _len);
        }
        this.printShow = true;
      },
      // 设置发货状态
      setStatus() {
        if (this.multipleSelection.length === 0) {
          this.$message.error('请选择要操作的项目!');
          return
        }
        let ids = this.multipleSelection.map(item => item.id).toString();
        this.loading = true;
        apiTree('erp/index', 'erp/setStatus', {'id': ids}, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '设置成功',
              type: 'success'
            });
            this.loading = false;
            this.multipleSelection = [];
            this.getErpList();
          }
        });
      },
      // 选择区域，显示小地块信息
      changeSelectArea() {
        this.checkedName = [];
        apiTree('erp/index', 'field/getSingleAreaName', {type: 'all', area_name: this.area_name}).then((res) => {
          if (res.s === 1) {
            res.d.data.forEach((item, index) => {
              this.mapNames.push(item.name);
            })
          }
        })
      },
      // 获得区域名
      getAreaName() {
        this.areaName = []
        apiTree('erp/index', 'field/getAreaName').then((res) => {
          if (res.s === 1) {
            res.d.data.forEach((item) => {
              this.areaNames.push(item.area_name)
            });
          }
        })
      },
      // 列表数据
      getErpList() {
        let param = this.$route.query
        if (util.isEmptyObject(param)) {
          this.selectItem = {
            page: 1,
            query: '',
            keywords: '',
            status: 0,
            order_type: 0,
            startTime: 0,
            endTime: 0
          }
        }
        this.loading = true
        NProgress.start()
        apiTree('erp/index', 'erp/index', param).then((res) => {
          if (res.s === 1) {
            this.total = res.d.total_num
            this.erpData = res.d.data
            this.selectItem.startTime = res.d.startTime;
            this.selectItem.endTime = res.d.endTime;
            this.loading = false
            NProgress.done()
          }
        })
      }
    },
    created() {
      this.getErpList();
      this.getAreaName();
    },
    watch: {
      $route: 'getErpList'
    }
  }

</script>

<style scoped lang="scss">
  .h-top {
    display: inline-block;
    width: 200px;
  }
</style>